﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.Mapping
{
    [CPPEI.WebApi.AuthGetAction("Mapping", "GetDataTableList", "1.0")]
    public class GetDataTableListActionAction : BaseAction<GetDataTableListActionParams, GetDataTableListActionResult>
    {
        protected override ActionResult<GetDataTableListActionResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataTableListActionParams opParams)
        {
            SER.DtTableQueryService service = new SER.DtTableQueryService();
            if(opParams.Type == 1)
            {
                PagedList<ENT.EDtTable> result = service.GetUnmappedTables(opParams.Filter, opParams.Page * 15, 15);
                if (result.IsSuccess)
                {
                    GetDataTableListActionResult actResult = new GetDataTableListActionResult();
                    actResult.Count = result.Data.Count;
                    actResult.Total = result.Total;
                    actResult.Limit = result.Limit;
                    actResult.Start = result.Start;
                    actResult.Page = opParams.Page;

                    foreach (var eItem in result.Data)
                    {
                        GetDataTableListActionResult.Item item = new GetDataTableListActionResult.Item();
                        item.Id = eItem.Id;
                        item.Name = eItem.Name;
                        item.Creator = eItem.Creator;
                        item.CreatedTime = eItem.CreatedTime;
                        item.IsRawDt = eItem.IsRawDt;
                        actResult.Items.Add(item);
                    }
                    return new ActionResult<GetDataTableListActionResult>(actResult);
                }
                else
                {
                    return new ActionResult<GetDataTableListActionResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
                }
            }
            else if(opParams.Type == 2)
            {
                PagedList<ENT.EDtTable> result = service.GetMappedTables(opParams.Filter, opParams.Page * 15, 15);
                if (result.IsSuccess)
                {
                    GetDataTableListActionResult actResult = new GetDataTableListActionResult();
                    actResult.Count = result.Data.Count;
                    actResult.Total = result.Total;
                    actResult.Limit = result.Limit;
                    actResult.Start = result.Start;
                    actResult.Page = opParams.Page;

                    foreach (var eItem in result.Data)
                    {
                        GetDataTableListActionResult.Item item = new GetDataTableListActionResult.Item();
                        item.Id = eItem.Id;
                        item.Name = eItem.Name;
                        item.Creator = eItem.Creator;
                        item.CreatedTime = eItem.CreatedTime;
                        item.IsRawDt = eItem.IsRawDt;
                        actResult.Items.Add(item);
                    }
                    return new ActionResult<GetDataTableListActionResult>(actResult);
                }
                else
                {
                    return new ActionResult<GetDataTableListActionResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
                }
            }
            return new ActionResult<GetDataTableListActionResult>(false, 2, "错误的参数", null);
        }
    }

    public class GetDataTableListActionParams
    {
        /// <summary>
        /// 1未映射，2已映射
        /// </summary>
        public int Type { get; set; }
        public string Filter { get; set; }
        public int Page { get; set; }
    }

    public class GetDataTableListActionResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Creator { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool IsRawDt { get; set; }
        }
    }
}
